﻿using System;
using System.IO;
using System.Text;

namespace NougakudoCompanion.Utils
{
    static public class Logging
    {
        static string GetLogFile()
        {
            //string log = @"C:\logs";
            string log = "";
            log = ShortcutPathUtility.GetShortcutRoot();
            if (!Directory.Exists(log))
            {
                // The logs folder used by Windows Azure VM.
                log = Path.Combine(ShortcutPathUtility.GetLocalResourceRoot(), "logs");
                if (!Directory.Exists(log))
                    Directory.CreateDirectory(log);
            }
            return Path.Combine(log, "NougakuDoRole.txt");
        }

        static public void Write(string msg)
        {
            var message = BuildMsg(msg);
            Console.WriteLine(message);
            using (var st = File.Open(GetLogFile(), FileMode.Append))
            {

                using (var tw = (TextWriter)new StreamWriter(st))
                {
                    tw.WriteLine(BuildMsg(msg));

                    tw.Close();
                }
                st.Close();
            }
        }

        static string BuildMsg(string msg)
        {
            StringBuilder buffer = new StringBuilder();
            buffer.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            buffer.Append("\t");
            buffer.Append(msg);
            return buffer.ToString();
        }
    }
}